package com.chenfan.mcn.component.dict;

import com.chenfan.mcn.extension.log.FieldDesc;
import com.chenfan.mcn.clients.baseinfo.BaseInfoClient;
import com.chenfan.mcn.clients.baseinfo.DescByCodeDTO;
import com.chenfan.common.extension.context.SpringContextHolder;
import com.chenfan.common.extension.dict.DictTranslator;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 数据字典翻译
 * @author wulg
 * @date 2023/3/29
 */
public class DataDictTranslator implements DictTranslator<Map<FieldDesc, ImmutablePair<String, String>>, Map<String, String>> {

    @Override
    public Map<String, String> translate(Map<FieldDesc, ImmutablePair<String, String>> map) {
        if(MapUtils.isEmpty(map)) {
            return Collections.EMPTY_MAP;
        }
        BaseInfoClient baseInfoClient = SpringContextHolder.getBean(BaseInfoClient.class);
        List<String> profileCodes = map.keySet().stream().map(FieldDesc::dictType).distinct().collect(Collectors.toList());
        List<String> dictsCodes = map.values().stream().flatMap(e-> Stream.of(e.getLeft(), e.getRight())).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
        return Objects.requireNonNull(baseInfoClient.getDescByCode(new DescByCodeDTO()
                .setDictsCodes(dictsCodes).setProfileCodes(profileCodes))).getObj();
    }
}